//
//  settingDetailViewController.m
//  one_mile_01
//
//  Created by 王进帅 on 15/11/2.
//  Copyright © 2015年 王雅蓉. All rights reserved.
//

#import "settingDetailViewController.h"

@interface settingDetailViewController ()

@end

@implementation settingDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = BACKGROUND_ASSISTANT_COLOR;
    self.navigationController.navigationBar.hidden = YES;
    
    UIView *navigationV = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, NAVIGATIONHEIGHT)];
    navigationV.backgroundColor = BACKGROUND_MAIN_COLOR;
    [self.view addSubview:navigationV];

    UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(WIDTH / 2.0 - NAVIGATION_TITLE_WIDTH / 2.0, NAVIGATIONHEIGHT - NAVIGATION_TITLE_HEIGHT - 5, NAVIGATION_TITLE_WIDTH, NAVIGATION_TITLE_HEIGHT)];
    titleLabel.text = @"设置";
    titleLabel.font = [UIFont systemFontOfSize:20.0f];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.textColor = [UIColor whiteColor];
    [self.view addSubview:titleLabel];
    
    //    返回按钮
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = POP_FRAME;
    [button setBackgroundImage:[UIImage imageNamed:@"pop_light@2x.png"] forState:UIControlStateNormal];
    button.backgroundColor = [UIColor clearColor];
    [button addTarget:self action:@selector(settingDetailBackButton:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
    
    self.settingDetailTableView = [[UITableView alloc] init];
    if (iPhone6) {
        
        self.settingDetailTableView.frame = CGRectMake(0, NAVIGATIONHEIGHT, self.view.frame.size.width, 45 * 2);
    }else{
        
        self.settingDetailTableView.frame = CGRectMake(0, NAVIGATIONHEIGHT, self.view.frame.size.width, 45 * HEIGHT / 667 * 2);
    }
    self.settingDetailTableView.backgroundColor = RGBACOLOR(240, 239, 245, 1.0);
    self.settingDetailTableView.delegate = self;
    self.settingDetailTableView.dataSource = self;
    self.settingDetailTableView.scrollEnabled = NO;
    [[UITableViewHeaderFooterView appearance] setTintColor:RGBACOLOR(240, 239, 245, 1.0)];
    [self.view addSubview:self.settingDetailTableView];

    // Do any additional setup after loading the view.
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
        return 2;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (iPhone6)
    {
        return 45;
    }else
        
    return HEIGHT * 45 / 667;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"myCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;//取消cell点击效果
        cell.backgroundColor = [UIColor whiteColor];
        cell.textLabel.font = [UIFont systemFontOfSize:17 *FITRATE];
        if (indexPath.row == 0) {
            
        }else{
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        }
    }
    
    if (indexPath.row == 0) {
        
        cell.textLabel.text = @"清理存储空间";
    } else if (indexPath.row == 1) {
        
        cell.textLabel.text = @"关于我们";
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
     if (indexPath.row == 1) {
        
        aboutUsViewController *aboutUsVC = [[aboutUsViewController alloc] init];
        [self.navigationController pushViewController:aboutUsVC animated:YES];
         
     }else if (indexPath.row == 0) {
         
        NSUInteger size = [[SDImageCache sharedImageCache] getSize];
        
        CGFloat mb = size / 1024.0 / 1024.0;
        NSString *imageSize = [NSString stringWithFormat:@"缓存数据:%.2fMB",mb];
         
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:imageSize message:@"是否清除缓存数据" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
        [alert show];
        
        [self.settingDetailTableView reloadData];
    }
}


#pragma mark -- alertViewDelegate
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if ([alertView.message isEqualToString:@"是否清除缓存数据"]) {
        
        [[SDImageCache sharedImageCache] cleanDisk];
        
        if (buttonIndex == 1) {
            
            dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
                
                NSString *cachPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0];
                NSArray *files = [[NSFileManager defaultManager] subpathsAtPath:cachPath];
                
                for (NSString *p in files) {
                    
                    NSError *error;
                    NSString *path = [cachPath stringByAppendingPathComponent:p];
                    if ([[NSFileManager defaultManager] fileExistsAtPath:path]) {
                        
                        [[NSFileManager defaultManager] removeItemAtPath:path error:&error];
                    }
                }
                [self performSelectorOnMainThread:@selector(clearCacheSuccess) withObject:nil waitUntilDone:YES];
            });
        }
    }
}

//清除缓存成功
-(void)clearCacheSuccess
{
    [self showAlert:@"清除成功"];
}


#pragma mark -- 警示框回弹自动
-(void)showAlert:(NSString *)message{//时间
    
    UIAlertView *promptAlert = [[UIAlertView alloc] initWithTitle:@"提示" message:message delegate:nil cancelButtonTitle:nil otherButtonTitles:nil];
    
    [NSTimer scheduledTimerWithTimeInterval:1.0f
                                     target:self
                                   selector:@selector(timerFireMethod:)
                                   userInfo:promptAlert
                                    repeats:YES];
    [promptAlert show];
}

- (void)timerFireMethod:(NSTimer *)theTimer//弹出框
{
    UIAlertView *promptAlert = (UIAlertView * )[theTimer userInfo];
    [promptAlert dismissWithClickedButtonIndex:0 animated:NO];
    promptAlert = NULL;
}

-(void)settingDetailBackButton:(UIButton *)button{
    
    [self.navigationController popToRootViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
